// DlgKeyboard.cpp : implementation file
//

#include "stdafx.h"
#include "resourcemake.h"
#include "resourcemakedlg.h"
#include "DlgKeyboard.h"
#include "Pane.h"
#include "XPButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgKeyboard dialog


CDlgKeyboard::CDlgKeyboard(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgKeyboard::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgKeyboard)
	//}}AFX_DATA_INIT
}


void CDlgKeyboard::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgKeyboard)
	DDX_Control(pDX, IDC_BTN_NEXT_SCENE, m_btnNextScene);
	DDX_Control(pDX, IDC_BTN_YELLOW, m_btnYellow);
	DDX_Control(pDX, IDC_BTN_BLUE, m_btnBlue);
	DDX_Control(pDX, IDC_BTN_GREEN, m_btnGreen);
	DDX_Control(pDX, IDC_BTN_RED, m_btnRed);
	DDX_Control(pDX, IDC_BTN_PRE_SCENE, m_btnPrevScene);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgKeyboard, CDialog)
	//{{AFX_MSG_MAP(CDlgKeyboard)
	ON_BN_CLICKED(IDC_BTN_RED, OnBtnRed)
	ON_BN_CLICKED(IDC_BTN_GREEN, OnBtnGreen)
	ON_BN_CLICKED(IDC_BTN_YELLOW, OnBtnYellow)
	ON_BN_CLICKED(IDC_BTN_BLUE, OnBtnBlue)
	ON_BN_CLICKED(IDC_BTN_PRE_SCENE, OnBtnPreScene)
	ON_BN_CLICKED(IDC_BTN_NEXT_SCENE, OnBtnNextScene)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgKeyboard message handlers

void CDlgKeyboard::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

void CDlgKeyboard::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	//CDialog::OnCancel();
}

void CDlgKeyboard::OnBtnRed() 
{
	KeyDown(1);
}

void CDlgKeyboard::OnBtnGreen() 
{
	KeyDown(2);
}

void CDlgKeyboard::OnBtnYellow() 
{
	KeyDown(3);
}

void CDlgKeyboard::OnBtnBlue() 
{
	KeyDown(4);
}

void CDlgKeyboard::KeyDown(int nKeyType)
{
	// TODO: Add your message handler code here and/or call default
	DDragDlg* m_ddd = ((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_ddd;
	CPane* m_pCurrPane = m_ddd->m_pCurrPane;
	if(m_pCurrPane==NULL)
		return;
	CPtrList& m_ObjectList = m_pCurrPane->m_ObjectList;

	POSITION pos = m_ObjectList.GetTailPosition();
	CDrawElement* pHitItem;
	while(pos != NULL)
	{
		POSITION curpos=pos;
		pHitItem=(CDrawElement*)m_ObjectList.GetPrev(pos);
		if(pHitItem != NULL)
		{
			if(pHitItem->m_Type==SHARP_KEY)
			{
				CKey* pKey = (CKey*)pHitItem;
				if(pKey->KeyType != nKeyType)
					continue;

				CString str = pKey->getHref();
				int nPaneID = atoi(str.Right(1));
				
				POSITION pos = m_ddd->m_PanList.GetHeadPosition();
				int iiic=0;
				
                HTREEITEM  hRoot=((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_probasedlg->m_paneTree->m_pPaneList->GetRootItem();
				HTREEITEM  h1=((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_probasedlg->m_paneTree->m_pPaneList->GetChildItem(hRoot);
				for(int i=1; i<nPaneID; i++)
				{
					h1 = ((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_probasedlg->m_paneTree->m_pPaneList->GetNextSiblingItem(h1);
				}
				((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_probasedlg->m_paneTree->m_pPaneList->SelectItem(h1);

				return;
			}
		}
	}
}

void CDlgKeyboard::OnBtnPreScene() 
{
	((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->ShowPrevScene();
}

void CDlgKeyboard::OnBtnNextScene() 
{
		((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->ShowNextScene();
}
